//
// Created by JYL on 2023-11-03.
//
#include "key.h"

#define KB1 HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)
#define KB2 HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)
#define KB3 HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2)
#define KB4 HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)
#define KEY_PORT (KB1|(KB2<<1)|(KB3<<2)|(KB4<<3)|0xf0)

/*
 * Function : (1) No key                :ReadData=0x00;Trg=0x00;Cont=0x00;
 *            (2) B1 is 0               :KEY_PORT=0xfe;ReadData=0x01;Trg=0x01;Cont=0x01;
 *            (3) when B1 is always 0   :Trg=0x01&(0x01^0x01)=0x00;Cont=0x01;
 *            (4) when B1 is 1          :Trg=0x00&(0x00^0x01)=0x00;Cont=0x00;
 * Example  :
 *          if(Trg & 0x01)//one key
 *          {//do something}
 *
 *          if(Cont & 0x01)//long key
 *          {
 *              if(++cnt_key_time==100)
 *              {
 *                  cnt_key_time=0;
 *                  //do something
 *              }
 *          }
 * */

uint8_t Trg;    //单次触发
uint8_t Cont;   //长按
void Key_Read(void)
{
    uint8_t ReadData = (KEY_PORT)^0xff;
    Trg = ReadData & (ReadData ^ Cont);
    Cont = ReadData;
}
